package org.openbpm.bus.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.openbpm.base.core.id.IdUtil;
import org.openbpm.bus.mapper.BusAmendBpmNodeDataMapper;
import org.openbpm.bus.model.BusAmendBpmNodeData;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class BusAmendBpmNodeDataService extends ServiceImpl<BusAmendBpmNodeDataMapper, BusAmendBpmNodeData> {
    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;


    //保存编制相关流程错误日志信息
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void asyncSaveOrUpdate(Long amendId, String amendNum, Long busId, String nodeCode, String dataJson) {
        threadPoolTaskExecutor.execute(() -> {
            this.saveOrUpdate(amendId, amendNum, busId, nodeCode, dataJson);
        });
    }

    public void saveOrUpdate(Long amendId, String amendNum, Long busId, String nodeCode, String dataJson) {

        BusAmendBpmNodeData data = baseMapper.selectOne(Wrappers.<BusAmendBpmNodeData>lambdaQuery()
                .eq(BusAmendBpmNodeData::getAmendid, amendId)
                .eq(BusAmendBpmNodeData::getNodeCode, nodeCode));

        if (data != null) {
            data.setAmendid(amendId);
            data.setAmendnum(amendNum);
            data.setBusId(busId);
            data.setNodeCode(nodeCode);
            data.setDataJson(dataJson);
            baseMapper.updateById(data);
        } else {
            data = new BusAmendBpmNodeData();
            data.setId(IdUtil.getUId());
            data.setAmendid(amendId);
            data.setAmendnum(amendNum);
            data.setBusId(busId);
            data.setNodeCode(nodeCode);
            data.setDataJson(dataJson);
            data.setPostdate(new Date());
            baseMapper.insert(data);
        }
    }

    public BusAmendBpmNodeData getBusAmendBpmNodeData(Long amendId, Long busId, String nodeCode) {
        return baseMapper.selectOne(Wrappers.<BusAmendBpmNodeData>lambdaQuery()
                .eq(BusAmendBpmNodeData::getAmendid, amendId)
                .eq(BusAmendBpmNodeData::getBusId, busId)
                .eq(BusAmendBpmNodeData::getNodeCode, nodeCode));
    }

    public String getDataJson(Long amendId, Long busId, String nodeCode) {
        BusAmendBpmNodeData data = null;
        if (busId == null || busId == 0) {
            data = baseMapper.selectOne(Wrappers.<BusAmendBpmNodeData>lambdaQuery()
                    .eq(BusAmendBpmNodeData::getAmendid, amendId)
                    .eq(BusAmendBpmNodeData::getNodeCode, nodeCode));
        } else {
            data = baseMapper.selectOne(Wrappers.<BusAmendBpmNodeData>lambdaQuery()
                    .eq(BusAmendBpmNodeData::getAmendid, amendId)
                    .eq(BusAmendBpmNodeData::getBusId, busId)
                    .eq(BusAmendBpmNodeData::getNodeCode, nodeCode));
        }
        return data != null ? data.getDataJson() : null;
    }
}
